package com.hsj.goldstore.mvp.view.fragment;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.Loader;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hsj.goldstore.R;
import com.hsj.goldstore.base.BaseMvpFrament;
import com.hsj.goldstore.base.PresenterFactory;
import com.hsj.goldstore.base.PresenterLoader;
import com.hsj.goldstore.mvp.present.fragment.GoldStorePresent;
import com.hsj.goldstore.utils.Helper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/25.
 */

public class GoldStoreFragment extends BaseMvpFrament<GoldStorePresent, GoldStoreView> implements GoldStoreView {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_right)
    TextView btnRight;
    @BindView(R.id.iv_store)
    ImageView ivStore;
    @BindView(R.id.tv_store_name)
    TextView tvStoreName;
    @BindView(R.id.iv_star)
    ImageView ivStar;
    @BindView(R.id.btn_go_next)
    ImageView btnGoNext;
    @BindView(R.id.iv_call_phone)
    ImageView ivCallPhone;
    @BindView(R.id.tv_scope_of_buiness)
    TextView tvScopeOfBuiness;
    @BindView(R.id.tv_store_phone)
    TextView tvStorePhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_advert)
    TextView tvAdvert;
    Unbinder unbinder;

    @Override
    protected int getResouseId() {

        return R.layout.fragment_gold_store;
    }

    @Override
    protected void initView() {
        tvTitle.setText("金店");
        btnRight.setVisibility(View.GONE);
        btnBack.setVisibility(View.GONE);
        ivStore.setImageResource(R.mipmap.ic_zb_bg2);
    }

    @Override
    protected void initData() {
        presenter.initData();
    }

    @Override
    public Loader<GoldStorePresent> onCreateLoader(int id, Bundle args) {
        return new PresenterLoader<>(getActivity(), new PresenterFactory<GoldStorePresent>() {
            @Override
            public GoldStorePresent create() {
                return new GoldStorePresent();
            }
        });
    }

    @Override
    public void showInfo() {

    }

    @Override
    public void callPhone(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        startActivity(intent);
    }

    @Override
    public String getPhone() {
        return tvStorePhone.getText().toString().trim();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.btn_right, R.id.btn_go_next, R.id.iv_call_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_right:
                break;
            case R.id.btn_go_next:
                break;
            case R.id.iv_call_phone:
                checkPermission();
                break;
        }
    }

    private static final int PEMISSIN_PHONE = 0x1123;
    private void checkPermission() {
        //
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {
            //第一次申请权限，如果用户拒绝，下次申请需向用户解释为什么要这个权限
            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity()
                    , Manifest.permission.CALL_PHONE)) {
                new AlertDialog.Builder(getActivity())
                        .setMessage("申请打电话权限")
                        .setPositiveButton("确定"
                                , new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //申请相机权限
                                ActivityCompat.requestPermissions(getActivity(),
                                        new String[]{Manifest.permission.CALL_PHONE}, PEMISSIN_PHONE);
                            }
                        })
                        .show();
            } else {
                //申请相机权限
                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{Manifest.permission.CAMERA}, PEMISSIN_PHONE);
            }
        } else {
            //已有权限 直接打开相机
            callPhone("10086");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == PEMISSIN_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                callPhone("10086");
            } else {
                Helper.showToast(getActivity(), "请开启拨打电话权限");
            }
        }
    }
}
